<?php
include_once("DataHelper.php");
include_once("GianHangDTO.php");

class GianHangDAO
{
	public static function ParseToGianHangDTO($row)
	{
		$gianHangDTO = new GianHangDTO();
		$gianHangDTO->gh_ma = $row["gh_ma"];
		$gianHangDTO->gh_ten = $row["gh_ten"];
		$gianHangDTO->gh_header = $row["gh_header"];
		$gianHangDTO->gh_ngay_tao = $row["gh_ngay_tao"];
		$gianHangDTO->gh_banner = $row["gh_banner"];
		$gianHangDTO->gh_footer = $row["gh_footer"];
		$gianHangDTO->gh_dia_chi = $row["gh_dia_chi"];
		$gianHangDTO->gh_username = $row["gh_username"];
		return $gianHangDTO;
	}
	public static function ParseToThichGianHangDTO($row)
	{
		$ThichGianHangDTO = new ThichGianHangDTO();
		$ThichGianHangDTO->tgh_ma_gian_hang = $row["gh_ma"];
		$ThichGianHangDTO->tgh_soluongthich = 0;
		return $ThichGianHangDTO;
	}
	
	public static function LayGianHangTheoUsername($username)
	{
		$sql = "SELECT * FROM gian_hang WHERE gh_username = '$username'";
		$result = QuerySQL($sql);
		
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$gianhangDTO = GianHangDAO::ParseToGianHangDTO($row);
			$arrayGianHangDTO[$i] = $gianhangDTO;
			$i = $i + 1;
		}
		return $arrayGianHangDTO;
		StopConnection();
	}
	public static function PartseToGianHangDTO1($row)
		{
			$ghDTO=new GianHangDTO();
			$ghDTO->tk_username= $row["tk_username"];
			$ghDTO->tk_password= $row["tk_password"];
			$ghDTO->tk_ngay_tao= $row["tk_ngay_tao"];
			$ghDTO->tk_email= $row["tk_email"];
			$ghDTO->tk_ho_ten= $row["tk_ho_ten"];
			$ghDTO->tk_gioi_tinh= $row["tk_gioi_tinh"];
			$ghDTO->tk_dia_chi= $row["tk_dia_chi"];
			$ghDTO->tk_dien_thoai= $row["tk_dien_thoai"];
			$ghDTO->tk_avatar= $row["tk_avatar"];
			$ghDTO->tk_tinh_trang= $row["tk_tinh_trang"];
			$ghDTO->tk_loai= $row["tk_loai"];
			$ghDTO->gh_ma= $row["gh_ma"];
			$ghDTO->gh_ten= $row["gh_ten"];
			$ghDTO->gh_ngay_tao= $row["gh_ngay_tao"];
			$ghDTO->gh_header= $row["gh_header"];
			$ghDTO->gh_banner= $row["gh_banner"];
			$ghDTO->gh_footer= $row["gh_footer"];
			$ghDTO->gh_dia_chi= $row["gh_dia_chi"];
			return $ghDTO;
		}
		public static function LayThongTinGianHangVaChuTheoMaGH($magh)
		{
			$sql="Select * From gian_hang gh,tai_khoan tk Where gh.gh_ma='$magh' and gh.gh_username=tk.tk_username";
			$result = QuerySQL($sql);
			$ghDTO=null;
			while($row = mysql_fetch_array($result))
			{
				$ghDTO = GianHangDAO::PartseToGianHangDTO1($row);
			}
			StopConnection();
			return $ghDTO;
		}
		public static function DemSoNguoiThichGianHang($magh)
		{
			$sql="Select count(*) as soluong From thich_gian_hang Where tgh_ma_gian_hang='$magh'";
			$result = QuerySQL($sql);
			$ghDTO=null;
			while($row = mysql_fetch_array($result))
			{
				$ghDTO = $row["soluong"];
			}
			StopConnection();
			return $ghDTO;
		}
		
		
		
		
		public static function KiemTraThichGianHang($magh,$username)
		{
			$sql="Select count(*) as soluong From thich_gian_hang Where tgh_ma_gian_hang='$magh' and tgh_username='$username'";
			$result = QuerySQL($sql);
			$ghDTO=null;
			while($row = mysql_fetch_array($result))
			{
				$ghDTO = $row["soluong"];
			}
			StopConnection();
			return $ghDTO;
		}
		public static function KiemTraDangKyNhanTin($magh,$username)
		{
			$sql="Select count(*) as soluong From dang_ky_nhan_tin Where dknt_ma_gian_hang='$magh' and dknt_username='$username'";
			$result = QuerySQL($sql);
			$ghDTO=null;
			while($row = mysql_fetch_array($result))
			{
				$ghDTO = $row["soluong"];
			}
			StopConnection();
			return $ghDTO;
		}
		
		public static function ThemDangKyNhanTin($dk)
		{
			$sql="Insert into dang_ky_nhan_tin(dknt_username,dknt_ma_gian_hang,dknt_ngay_tao) values('$dk->dknt_username','$dk->dknt_ma_gian_hang','$dk->dknt_ngay_tao')";
			$result = QuerySQL($sql);
			StopConnection();
			return $result;
		}
		public static function XoaDangKyNhanTin($username,$magh)
		{
			$sql="Delete From dang_ky_nhan_tin Where dknt_username='$username' and dknt_ma_gian_hang='$magh'";
			$result = QuerySQL($sql);
			StopConnection();
			return $result;
		}
		
		public static function ThemThichGianHang($username,$magh)
		{
			$sql="Insert into thich_gian_hang(tgh_username,tgh_ma_gian_hang) values('$username','$magh')";
			$result = QuerySQL($sql);
			StopConnection();
			return $result;
		}
		public static function XoaThichGianHang($username,$magh)
		{
			$sql="Delete From thich_gian_hang Where tgh_username='$username' and tgh_ma_gian_hang='$magh'";
			$result = QuerySQL($sql);
			StopConnection();
			return $result;
		}
		
		public static function GianHangHot()
		{
			$sqlString = "SELECT tgh_ma_gian_hang, count( tgh_username )as soluong
							FROM `thich_gian_hang`
							GROUP BY tgh_ma_gian_hang
							order by soluong desc
							Limit 0, 4";
			$results = QuerySQL($sqlString);
			$i = 0;
			while($row = mysql_fetch_array($result))
			{
				$spDTO = ThichGianHangDAO::ParseToThichGianHangDTO($row);
				$arraySpDTO[$i] = $spDTO;
				$i = $i + 1;
			}
			StopConnection();
			return $arraySpDTO;
		}
	}
?>